package com.itgo.codegen;

import java.io.FileInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itgo.codegen.bean.TableMeta;
import com.itgo.codegen.config.Config;
import com.itgo.codegen.gen.BeanGen;
import com.itgo.codegen.gen.actiongen.ActionGen;
import com.itgo.codegen.gen.actiongen.ConfigParam;
import com.itgo.codegen.gen.actiongen.TemplateParam;
import com.itgo.codegen.gen.daogen.DaoGen;
import com.itgo.codegen.gen.servicegen.ServiceGen;
import com.itgo.codegen.util.DBMetaUtil;
import com.itgo.codegen.util.RuntimeUtil;
import com.itgo.codegen.util.StringLang;

public class CodeGenMain { 
	
	public static ConfigParam getConfig(){
		ConfigParam param = null ;
		try{
			ObjectMapper mapper = new ObjectMapper() ;
			param = mapper.readValue( new FileInputStream("./config.json")  , ConfigParam.class ) ;
		}catch(Exception e){
			e.printStackTrace();
		}
		return param ;
	}
	
	/**
	 * 1、生成基本增删该查
	 * 		1）、状态字段
	 * 		2）、删除标识字段
	 * 2、增加基本逻辑判断
	 * 		1）、重复校验（校验字段）
	 * 
	 * ------------------
	 * 
	 * 删除标识
	 * 重复校验
	 * 生成方法（查询列表、查询单个、新增、修改、删除、更新状态）
	 */
	public static void main( String args [] ){
		ConfigParam param = getConfig() ;
		
		if( param == null || param.getGens() == null || param.getGens().isEmpty() )
			return ;
		
		//config jdbc connection .
		Config.dbUserName = param.getDb().get("userName") ;
		Config.dbPassword = param.getDb().get("password") ;
		Config.jdbcUrl = param.getDb().get("jdbcUrl") ;
		Config.dbName = param.getDb().get("database") ;
		
		//output path .
		Config.targetPath = param.getOutput() ;
		
		for( TemplateParam tpl : param.getGens() ){
			Config.tableName = tpl.getTable() ;
			TableMeta tableMeta = DBMetaUtil.getTableMeta();
			
			Config.templateParam = tpl ;
			
			//生成Bean
			BeanGen beanGen = new BeanGen( tableMeta ) ;
			beanGen.gen( String.format("%s/%s.java", Config.targetPath ,
					StringLang.au(tpl.getObject() ) ) , "./tpl/bean.tpl") ;
			
			//生成Action
			ActionGen actionGen = new ActionGen( ) ;
			actionGen.gen( String.format("%s/%sAction.java", Config.targetPath ,
					StringLang.au(tpl.getObject() ) ), "./tpl/action.tpl");
			
			//生成Service
			ServiceGen serviceGen = new ServiceGen( ) ;
			serviceGen.gen( String.format("%s/%sService.java", Config.targetPath ,
					StringLang.au(tpl.getObject() ) ) , "./tpl/service.tpl");
			
			//生成Dao
			DaoGen daoGen = new DaoGen( ) ;
			daoGen.gen( String.format("%s/%sDao.java", Config.targetPath ,
					StringLang.au(tpl.getObject() ) ), "./tpl/dao.tpl");
		}
		RuntimeUtil.startExplorer( Config.targetPath ) ;
	}
}
 